#pragma once
#include <string>
#include <boost/date_time/posix_time/posix_time.hpp>
#include <odb/nullable.hxx>
#include <odb/core.hxx>


#pragma db object
class Student
{
    public:
    Student()
    {}

    ~Student() {}

    Student(unsigned long sn, const std::string name, unsigned short age, unsigned long class_id) 
    : _sn(sn), _name(name), _age(age), _class_id(class_id) {}

    void set_sn(unsigned long sn)  {_sn = sn;}
    unsigned long sn() {return _sn;}

    void set_name(const std::string& name) {_name = name;}
    std::string name() {return _name;}

    void set_age(unsigned short age) {_age = age;}
    odb::nullable<unsigned short> age() {return _age;};

    void set_class_id(unsigned long class_id) {_class_id = class_id;}
    unsigned long class_id() {return _class_id;};

private:
        friend odb::access;
        #pragma db id auto
        unsigned long _id;                          // 学生id

        #pragma db unique
        unsigned long _sn;                          // 序号
        
        std::string _name;                           // 名字
        
        odb::nullable<unsigned short> _age;         // 年龄
        
        #pragma db index
        unsigned long _class_id;                    // 班级id
};


#pragma db object
class Classes
{
public:
    Classes() {}
    Classes(const std::string &name) : _name(name) {}

    void set_name(const std::string &name) { _name = name; }
    std::string name() { return _name; }

private:
    friend odb::access;
#pragma db id auto
    unsigned long _id; // 班级id
    std::string _name; // 班级名字
};

// 查询所有学生的信息，并显示班级名称
#pragma db view object(Student)\
                object(Classes = classes : Student::_class_id == classes::_id)\
                query((?))
struct classes_student
{
    #pragma db column(Student::_id)
    unsigned long id;
    #pragma db column(Student::_sn)
    unsigned long sn;
    #pragma db column(Student::_name)
    std::string name;
    #pragma db column(Student::_age)
    odb::nullable<unsigned short> age;
    #pragma db column(classes::_name)
    std::string classes_name;
};

//只查询学生姓名，（？）表示传入的条件
#pragma db view query("select name from Student" + (?))
struct all_name
{
    std::string name;
};

//odb -d mysql --std c++11 --generate-query --generate-schema --profile boost/date-time student.hxx